package com.ashleyleung.jetpack_b_station.utils

import android.content.Context
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.constant.SpinnerStyle

/**
 * @author lxy
 * @name
 * @class describe
 * @date 2022-06-29
 * Copyright (c)  Guangzhou GCI Science & Technology Co., Ltd.All rights reserved
 */
object ViewInitUtil {
    fun initSmartRefreshLayout(context: Context, refreshLayout: SmartRefreshLayout) {
        refreshLayout.setRefreshHeader(ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate))
        refreshLayout.setRefreshFooter(ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate))
        refreshLayout.setEnableScrollContentWhenLoaded(true) //是否在加载完成时滚动列表显示新的内容
        refreshLayout.setEnableLoadMoreWhenContentNotFull(true) //是否在列表不满一页时候开启上拉加载功能
        refreshLayout.setEnableScrollContentWhenRefreshed(true) //是否在刷新完成时滚动列表显示新的内容 1.0.5
    }

    fun resetSmartRefreshLayout(smartRefreshLayout: SmartRefreshLayout) {
        smartRefreshLayout.finishLoadMore() //结束加载
        smartRefreshLayout.finishRefresh() //结束刷新
    }


    fun initLinearRecyclerView(context: Context, rcyList: RecyclerView) {
        val linearLayoutManager = LinearLayoutManager(context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        linearLayoutManager.isSmoothScrollbarEnabled = true
        rcyList.layoutManager = linearLayoutManager
        rcyList.setHasFixedSize(true)
        rcyList.isNestedScrollingEnabled = false
        val space = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            0.5f,
            context.resources.displayMetrics
        ).toInt()
        rcyList.addItemDecoration(object : ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                outRect[0, 0, 0] = space
            }
        })
    }


}